package View;

import Domain.Deck;
import Persistence.DeckRepository;
import Util.ErrorHelper;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.vargas.studypartner.NoteCardActivity;
import com.vargas.studypartner.R;

/**
 * New Deck dialog class that extends Android {@link Dialog}. 
 * Wires up the newdeck_dialog view.
 * 
 * Date			Description
 * 04/07/2012	Created
 * */
public class NewDeckDialog extends Dialog{

	private Context _Context;
	
	/**
	 * Default constructor. Sets the content view, title, and click listeners.
	 * @param	context		The application context.{@link Context}
	 * */
	public NewDeckDialog(Context context) {
		super(context, android.R.style.Theme_Dialog);
		setContentView(R.layout.newdeck_dialog);
		setTitle(R.string.txtNewDeck);
		
		_Context = context;
		
		Button btnAccept = (Button)findViewById(R.id.btnNewDeck_Accept);		
		btnAccept.setOnClickListener(CreateAcceptButtonClickListener());
		
		Button btnCancel = (Button)findViewById(R.id.btnNewDeck_Cancel);
		btnCancel.setOnClickListener(CreateCancelButtonClickListener());
	}
	
	/**
	 * Creates and returns a click listener for the Accept button. 
	 * @return {@link View.OnClickListener} 
	 * */
	private View.OnClickListener CreateAcceptButtonClickListener(){
		return new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String title = ((EditText)v.getRootView().findViewById(R.id.editDeckName)).getText().toString();
				if (TextUtils.isEmpty(title)){
					Toast.makeText(_Context, R.string.txtNoNameError, Toast.LENGTH_SHORT).show();
				} else {
					Deck newDeck = CreateNewDeck(title);
					StartDeckActivity(newDeck);
					NewDeckDialog.this.dismiss();
				}
			};
		};
	}
	
	/**
	 * Creates and returns a click listener for the Cancel button.
	 * @return {@link View.OnClickListener}		Click listener that cancels the dialog.
	 * */
	private View.OnClickListener CreateCancelButtonClickListener() {
		return new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				cancel();
			}
		};
	}
	
	/**
	 * Creates a new deck using the given titles and add one blank card then saves .
	 * @param	title	The title to give to the deck. {@link String}
	 * @return	{@link Deck}	A new deck instance with one blank card.
	 * */
	private Deck CreateNewDeck(String title) {
		Deck newDeck = new Deck();
		newDeck.setName(title);
		newDeck.addNewNoteCard();
		try{
			DeckRepository repo = new DeckRepository(_Context);
			Deck updatedDeck = repo.addRow(newDeck);
			return updatedDeck;
		} catch (Exception e) {
			ErrorHelper.reportError(NewDeckDialog.this.getClass(), _Context, e);
			cancel();
			return newDeck;
		}
	}
	
	/**
	 * Starts the Deck activity.
	 * @param	deck	The deck instance to use for activity.{@link Deck}
	 * */
	private void StartDeckActivity(Deck deck) {
		try{
			Intent deckActivity = new Intent(_Context, NoteCardActivity.class);
			deckActivity.putExtra("DeckID", deck.getID());
			_Context.startActivity(deckActivity);
		} catch(Exception e){
			ErrorHelper.reportError(NewDeckDialog.this.getClass(), _Context, e);
		}
	}
}
